#!BPY

""" Registration info for Blender menus:
Name: 'AGL 1.0 (.agl)...'
Blender: 245
Group: 'Export'
Submenu: 'All Meshes...' all
Submenu: 'Selected objects...' selected
Tooltip: 'Export selected mesh to AGL File (*.agl)'
"""

# +---------------------------------------------------------+
# | Copyright (c) 2008 Justin L. Boss                       |
# | http://www.rtechguys.com                                |
# | justin@rtechguys.com                                    |
# | Aprl 25, 2008                                           |
# +---------------------------------------------------------+

import Blender
from Blender import *
from math import *

class Export:
	def __init__(self, filename):
		self.file = open(filename, "wb")

	def closefile(self):
			self.file.close()

	def writeBoneData(self, object):
		mesh = object.data
		groups=mesh.getVertGroupNames()
		bones=[]
		smallest='\0'
		for group in groups:
			vert = mesh.getVertsFromGroup(group,1)
			for vertex in vert:
				if smallest > mesh.verts[vertex[0]].co[0]:
					smallest = mesh.verts[vertex[0]].co[0]
		bones.append(smallest)
		for group in groups:
			if groups[0] != group:
				vert = mesh.getVertsFromGroup(group,1)
				smallest = mesh.verts[vert[0][0]].co[0]
				for vertex in vert:
					if smallest > mesh.verts[vertex[0]].co[0]:
						smallest = mesh.verts[vertex[0]].co[0]
				bones.append(smallest)
		self.file.write("%i " %( len( bones ) ) )
		for bone in range( len( bones )):
			if bone > 0:
				self.file.write("%f " %(bones[bone]-bones[bone-1]))
		return bones
#End


	#EXPORT Vertex data
	def writeVertexData(self, object, bones):
		self.file.write("%i " %(len(object.data.verts)))
		mesh = object.data
		groups=mesh.getVertGroupNames()
		for vertex in range(len(mesh.verts)):
			for group in groups:
				gvert = mesh.getVertsFromGroup(group,1)
				for gvertex in gvert:
					if gvertex[0]==vertex:
						self.file.write("%f %f %f %f %f %f %f %i " %(mesh.verts[vertex].co[0]-bones[groups.index(group)], mesh.verts[vertex].co[1], mesh.verts[vertex].co[2],mesh.verts[vertex].no[0],mesh.verts[vertex].no[1],mesh.verts[vertex].no[2], gvertex[1], groups.index(group)))
#End

	#EXPORT Normal data
	def writeNormalData(self, object):
		self.file.write("\nGLfloat %s_Normal[][3] = {\n" %(object.name.replace(".", "")))
		mesh = object.data
		for vertex in mesh.verts:
			self.file.write("{%f, %f, %f},\n" %(vertex.no[0], vertex.no[1], vertex.no[2]))
#End

	#EXPORT Normal data
#	def writeNormalData(self, object):
#		self.file.write("\nGLfloat %s_Normal[][3] = {\n" %(object.name.replace(".", "")))
#		mesh = object.data
#		for face in mesh.faces:
#			self.file.write("{%f, %f, %f},\n" %(face.no[0], face.no[1], face.no[2]))
#		self.file.write("};\n")
#End

	#EXPORT UV data
	def writeUVData(self, object):
		self.file.write("%i " %(len(object.data.verts)))
		mesh = object.data
		index=0
		while index < len(mesh.verts):
			for face in mesh.faces:
				if face[0].index == index:
					self.file.write("%f %f " %(face.uv[0][0], face.uv[0][1]))
					index=index+1
				if face[1].index == index:
					self.file.write("%f %f " %(face.uv[1][0], face.uv[1][1]))
					index=index+1
				if face[2].index == index:
					self.file.write("%f %f " %(face.uv[2][0], face.uv[2][1]))
					index=index+1
				if face[3].index == index:
					self.file.write("%f %f " %(face.uv[3][0], face.uv[3][1]))
					index=index+1

	#EXPORT FACE DATA
	def writeFacesData(self, object):
		self.file.write("%i " %(len(object.data.faces)))
		mesh = object.data
		for face in mesh.faces:
			if len(face.v) == 3:
				self.file.write("%i %i %i %i " %(face[0].index, face[1].index, face[2].index, face[2].index))
			elif len(face.v) == 4:
				self.file.write("%i %i %i %i " %(face[0].index, face[1].index, face[2].index, face[3].index))
#End

	def All(self):
		scene = Blender.Scene.GetCurrent()
		objlist = scene.objects
		for objects in objlist:
			if objects.getType() == 'Mesh':
				self.writeVertexData (objects, self.writeBoneData(objects))
				self.writeUVData (objects)
				self.writeFacesData (objects)
				
	def Selected(self):
		objlist = Blender.Object.GetSelected()
		for objects in objlist:
			if objects.getType() == 'Mesh':
				self.writeVertexData (objects, self.writeBoneData(objects))
				self.writeUVData (objects)
				self.writeFacesData (objects)
#End

def fs_callback(filename):
	if filename.find('.agl', -4) <= 0: filename += '.agl'
	 
	AGL = Export (filename)
	arg = __script__['arg']
	#AGL.All()
	if arg == 'all':
		AGL.All()
	if arg == 'selected':
		AGL.Selected()
	AGL.closefile()

Blender.Window.FileSelector(fs_callback, "AGL Export") 